#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpx_supt.h"
#include "MOD1P.h"
#include <dos.h>
#include <dir.h>
#include <time.h>

/* static char array to store the current directory */
static char curr_path[MAX_PATH_SIZE];

/* Entry of ffblk  */
static struct ffblk file_block;

/* */
static int num_entries;



/*Version information*/
float vers = 1.5;
int modMonth = 10;
int modDay = 03;
int modYear = 2008;


/*global variables for readcmd (), processcmd()*/
static int x=1; /*x is our int length of command string, to be used later, y is used to keep track of number of paramaters*/
static char *scmd = NULL;
char par1[PARLENGTH],par2[PARLENGTH], par3[PARLENGTH],par4[PARLENGTH], par5[PARLENGTH]; 

int main(void)
{
      sys_init(MODULE_R2);

      date("get","");
      welcome();
/*dded: use an endless cycle to read, process commands.
//added:  match the input commands with our commands libray and
//added:  decide which functions should be loaded.*/

      queue_init();

      while (1)
     {
        readcmd();
	processcmd();


       if (!strcmp(par1,"help"))
       {
        printf("Load help function.\n");
        help(par2);
        }
       else if ( !strcmp(par1,"version"))
            {
              printf("Load version function.\n");
              version();
            }
       else if ( !strcmp(par1,"date"))
	     {
	      printf("Load date function.\n");
	      date(par2,par3);
	     }
       else if ( !strcmp(par1,"display") )
	       {
	      printf("Load display function.\n");
	      disp(par2);

	      }
       else if ( !strcmp(par1,"exit") )
               {
	      printf("Load exit function.\n");
              mpx_exit();

	      }
       else if(!strcmp(par1,"show")){
	      printf("Load show function.\n");

	      show(par2);

	      }
       else if(!strcmp(par1,"create")){
	      printf("Load insert PCB function.\n");
	      insertpcb(par2,par3,par4,par5);
	      }

       else if(!strcmp(par1, "delete")){
	      printf("Load delete PCB function.\n");
	      removepcb(par2, par3);
	      }
       else if(!strcmp(par1, "block")){
	      printf("Load block function.\n");
	      block(par2);
	      }
       else if(!strcmp(par1, "unblock")){
	      printf("Load unblock function.\n");
	      unblock(par2);
	      }

       else if(!strcmp(par1, "suspend")){
	      printf("Load suspend function.\n");
            suspend(par2);
	      }
       else if(!strcmp(par1, "resume")){
	      printf("Load resume function.\n");
	      resume(par2);
	      }
       else if(!strcmp(par1, "setpriority")){
	      printf("Load set priority function.\n");
	      setpriority(par2,par3);
	      }

	   else
	     {
	      printf("Unrecognized command.  Try again.\n");
	     }
      }

}


void welcome ()
{
	printf ("Welcome to MPX!\n");
	printf("The following commands can be input at the command prompt.\n");

	printf("-  date <set/get>\n");
	printf("-  display <directory>\n");
	printf("-  version\n");
	printf("-  help <function>\n");
	printf("-  exit\n");
	printf("-  create <PCB name> <class number> <queue name> <priority>\n");
	printf("-  delete <PCB name> <queue name>\n");
      printf("-  block <PCB name>\n");
      printf("-  unblock <PCB name>\n");
      printf("-  suspend <PCB name>\n");
      printf("-  resume <PCB name>\n");
      printf("-  setpriority <PCB name> <priority number>\n");
	printf("-  show <queue name>");
}


void readcmd()
{
	scmd=sys_alloc_mem(80*sizeof(scmd));
	printf("\n\nMPX>");
	gets(scmd);
}

/* The process function takes the string scmd, the command given by the user
// and passes it to the appropriate function with the proper paramaters*/

void processcmd()
{       int y=0;
	char *p;
	p = sys_alloc_mem(10*sizeof(char));
/*
	par1=sys_alloc_mem(15*sizeof(char));
	par2=sys_alloc_mem(15*sizeof(char));
	par3=sys_alloc_mem(15*sizeof(char));
*/ 
	p = strtok(scmd," ");

	strcpy(par1, p);
	p = strtok(NULL, " ,");
	while (p != NULL)
	{

		if (y==0){
		strcpy(par2, p);
		 }
		else if (y==1){
		 strcpy(par3, p);
		 }
              else if(y==2){
             strcpy(par4,p);
              }
             else if(y==3){
             strcpy(par5,p);
             }
		y++;
		p = strtok(NULL, " ,");
	}

	printf("%s : %s : %s\n", par1, par2, par3);
}


void mpx_exit()
{

        char ex;
        printf("ARE YOU SURE YOU WANT TO EXIT? ");
        scanf("%c", &ex);

	/*printf("the input is %c\n",ex);*/
        if(ex == 'y' || ex == 'Y')
        {
                printf("Now exiting. . . \n");
                exit(1);
        }
        else if(ex == 'n' || ex == 'N')
        {
                return;
        }
        else
        {
                printf("Invalid command.\n");
                return;
        }
}


void version()
{
    printf("Version Number: ");
    printf("%f\n",vers);
    printf("Last modified: ");
    printf("%d / %d / %d\n", modMonth, modDay, modYear);
}



void disp(char* directory)
{
  char namebuf[60];
  long filesize;

  if(directory==NULL){
     printf("Please specify a directory to search.");
     return;
     }

if (sys_open_dir (directory))
   printf("Can't open the file.\n");

while(!sys_get_entry ( namebuf, 60, &filesize))
{
printf ("%40s\t", namebuf);
printf ("%10ld\n", filesize);
}

if (sys_close_dir())
 printf("Can't close the directory.\n");

}


void help(char* func){


FILE *file;
char c;
char* fn;


fn = (char*)(sys_alloc_mem(25*sizeof(fn)));
strcat(fn,"HELP\\");

printf("Entering help...\n \n");

if(func == NULL)
{
   file=fopen("HELP\\COMMAND.TXT","r");
   if(file==NULL){
      printf("Cannot open command list.  Sorry :( \n \n");
      return;
   }
   else{
      while(!feof(file))
      {
       c = fgetc(file);
       fputc(c,stdout);
      }

      }

      printf("\n \n");

}

else
{
   strcat(fn,func);
   strcat(fn,".HLP");

   file=fopen(fn,"r");
   if(file==NULL)
    {
       printf("Cannot open specified function definition.  Sorry :( \n \n");
       return;
   }
   else
    {
      while(!feof(file))
      {
       c = fgetc(file);
       fputc(c,stdout);
      }

    }
      printf("\n \n");

}
fclose(file);
} /*end help*/


void date(char opt[6], char dte[20]){
   char *p;
   int mon, day, yr;
   int i=0, temp;
   date_rec aDate;

   if(strlen(opt)==3){
     sys_get_date(&aDate);

     mon=aDate.month;
     day=aDate.day;
     yr=aDate.year;

     if(strcmp("set",opt)==0){
      p=strtok(dte,"-");
      while(p!=NULL){
        temp=atoi(p);
        if(i==0){
          if(temp>=1 && temp<=12)
	    aDate.month=temp;
          else{
	    printf("\nNOT A VALID DATE! Invalid month. . . \n");
            return;
          }
        }
        if(i==1){
	  if((aDate.month==9 || aDate.month==4 || aDate.month==6 || aDate.month==11) && temp<=30){
           aDate.day=temp;

           }
          else if(aDate.month==2 && temp<=29){
           aDate.day=temp;

          }
          else if((aDate.month==1 || aDate.month==3 ||aDate.month==5 || aDate.month==7 || aDate.month==8 || aDate.month==10 || aDate.month==12) && temp<=31){
           aDate.day=temp;

          }
          else{
	   printf("\nNOT A VALID DATE!  Invalid day.\n");
           aDate.month=mon;
           aDate.day=day;
           aDate.year=yr;
           return;
           }
        }
        if(i==2){
          if(aDate.month==2 && aDate.day==29){
            if(temp%4==0){
              aDate.year=temp;
              printf("Date successfully set!");
              return;
	      }
	    else{
	      printf("\nNOT A VALID DATE!  Not a leap year.");
	      aDate.month=mon;
	      aDate.day=day;
	      aDate.year=yr;
	      return;
	      }
          }
	  else if(temp > 0 && temp < 9999){
            aDate.year=temp;
            printf("Date successfully set!");
	  }
	  else{
	      printf("\nNOT A VALID DATE!  Invalid year (must be a year between 0 and 9999).");
	      aDate.month=mon;
	      aDate.day=day;
	      aDate.year=yr;
	      return;
	      }
        }
        i++;
        p=strtok(NULL, "-");
      }

      sys_set_date(&aDate);

      }

      else if(strcmp("get",opt)==0){
      sys_get_date(&aDate);

      switch (aDate.month){
         case 1:
           printf("January %d, %u \n", aDate.day, aDate.year);
           break;
         case 2:
           printf("February %d, %d \n", aDate.day, aDate.year);
           break;
         case 3:
           printf("March %d, %d \n", aDate.day, aDate.year);
           break;
         case 4:
           printf("April %d, %d \n", aDate.day, aDate.year);
           break;
         case 5:
           printf("May %d, %d \n", aDate.day, aDate.year);
           break;
         case 6:
           printf("June %d, %d \n", aDate.day, aDate.year);
           break;
         case 7:
           printf("July %d, %d \n", aDate.day, aDate.year);
           break;
         case 8:
           printf("August %d, %d \n", aDate.day, aDate.year);
           break;
         case 9:
           printf("September %d, %d \n", aDate.day, aDate.year);
           break;
         case 10:
           printf("October %d, %d \n", aDate.day, aDate.year);
           break;
         case 11:
           printf("November %d, %d \n", aDate.day, aDate.year);
           break;
         case 12:
           printf("December %d, %d \n", aDate.day, aDate.year);
           break;
         case NULL:
           printf("Date has not yet been set.  Please set today's date.\n");
           break;
         default:
           printf("Invalid date.  Something went wrong here. . .\n");
           printf("Does this help? Month: %d, Day: %d, Year: %d\n", aDate.month, aDate.day, aDate.year);
           break;
        }
      }

      else{
         printf("Not a recognized command. . .");
      }

      }
      else
        printf("Not a recognized command. . .");
}


/********************************
The following is for mod2
*********************************/
/* allocate memory to PCB, return NULL if unsuccessful */ 
PCB* allo_pcb (void ){

PCB*  p;
p = (PCB*)sys_alloc_mem(sizeof(PCB)); 
return p; 

}


/*Initialize a PCB by providing class, name, and priority, state was set to READY as default */ 
PCB* set_pcb(PCB* p,char name[MAX_PCB_NAME], char *cla,char *queue, char *priority) {

if(p==NULL)
printf("Have not been assigned memory successfully.");

else
{
strcpy(p->pcb_name, name);
p->pcb_class = atoi(cla);
strcpy(p->pcb_state,queue);
p->pcb_priority=atoi(priority);

/* 
p->pcb_memory=NULL;  
p->pcb_resource=NULL;
p->pcb_context=NULL;
p->pcb_account=NULL;
*/
}

return p;
}


void free_pcb(PCB* p){
sys_free_mem( (void*)p );

}

/******************************
The following is for queues by linked list
*********************************/
ListPtr ReadyQueue;
ListPtr BlockQueue;
ListPtr SusReadyQueue;
ListPtr SusBlockQueue;

/* Beginning of functions*/
void queue_init(void){
ReadyQueue = sys_alloc_mem(sizeof(List));
ReadyQueue->head=NULL;
ReadyQueue->tail=NULL;

/* There is no waitqueue 
WaitQueue =  sys_alloc_mem(sizeof(List));
WaitQueue->head=NULL;
ReadyQueue->tail=NULL;
*/ 

BlockQueue = sys_alloc_mem(sizeof(List));
BlockQueue->head=NULL;
ReadyQueue->tail=NULL;

SusReadyQueue = sys_alloc_mem(sizeof(List));
SusReadyQueue->head=NULL;
ReadyQueue->tail=NULL;

SusBlockQueue = sys_alloc_mem(sizeof(List));
SusBlockQueue->head=NULL;
ReadyQueue->tail=NULL;

}

void insertpcb(char *value,char *cla, char *queue, char *priority){
/*creat a PCB by allocating memory and initialize it */
       PCB*  p;
       p = allo_pcb();
       p = set_pcb(p,value,cla,queue,priority);

	if(!strcmp(queue,"ready")){
	   insert(ReadyQueue, p);
	   return;
	}
	else 
     {

	if(!strcmp(queue, "blocked")){
	  insertfifo(BlockQueue, p);
        return;
	}
	else if(!strcmp(queue, "suspended-ready")){
	   
	      insertfifo(SusReadyQueue, p);
            return;
	   
	}
	else if(!strcmp(queue, "suspended-blocked")){
	   
	      insertfifo(SusBlockQueue, p);
           return;
	   
	}
	else{
	   printf("Invalid process state!\n");
	   return;
	}

    }
}

void removepcb(char *data, char *queue){
	if(!strcmp(queue,"ready")){
	   if(!isEmpty(ReadyQueue)){
	      if(dele(ReadyQueue, data)){
		 printf("%s deleted.\n", data);
		 }
	      else
		 printf("%s not found.\n", data);
	      }
	      else if(isEmpty(ReadyQueue)){
		 printf("List is empty.\n");
		 return;
	      }
	}

	
	else if(!strcmp(queue, "blocked")){
	   if(!isEmpty(BlockQueue)){
	      if(dele(BlockQueue, data)){
		 printf("%s deleted.\n", data);
		 }
	      else
		 printf("%s not found.\n", data);
	      }
	      else if(isEmpty(BlockQueue)){
		 printf("List is empty.\n");
		 return;
	      }
	}
	else if(!strcmp(queue, "suspended-ready")){
	   if(!isEmpty(SusReadyQueue)){
	      if(dele(SusReadyQueue, data)){
		 printf("%s deleted.\n", data);
		 }
	      else
		 printf("%s not found.\n", data);
	      }
	      else if(isEmpty(SusReadyQueue)){
		 printf("List is empty.\n");
		 return;
	      }
	}
	else if(!strcmp(queue, "suspended-blocked")){
	   if(!isEmpty(SusBlockQueue)){
	      if(dele(SusBlockQueue, data)){
		 printf("%s deleted.\n", data);
		 }
	      else
		 printf("%s not found.\n", data);
	      }
	      else if(isEmpty(SusBlockQueue)){
		 printf("List is empty.\n");
		 return;
	      }
	}
	else{
	   printf("Invalid process state!\n");
	   return;
	}
}
void show (char *option){
      
      PCB* p; 
	if(!strcmp(option, "all")){
	   printf("The following strings are contained in system queues:\n");
	   printf("*****************************************************\n");
	   printf("Ready:\n");
	   printList("ready");
	   printf("\n");
	   printf("Blocked:\n");
	   printList("blocked");
	   printf("\n");
	   printf("Suspended-Ready:\n");
	   printList("suspended-ready");
	   printf("\n");
	   printf("Suspended-Blocked:\n");
	   printList("suspended-blocked");
	}
	else if (isQueue(option))
     {
	   printf("The following strings are contained in the %s queue:\n",  option);
	   printf("****************************************************\n");
	   printList(option);
	}

     else if ( (p=findall(option))!=NULL ) 
    {
     print_pcb(p);
    }
    else
    printf("Can't print, please verify the name.\n");
        
}

int isQueue(char *option){
 if(!strcmp(option, "blocked"))
  return 1; 
 else if(!strcmp(option,"ready"))
  return 1; 
 else if(!strcmp(option,"suspended-ready"))
  return 1; 
 else if(!strcmp(option,"suspended-blocked"))
  return 1; 
 else
  return 0; 
}

void insert(ListPtr sPtr, PCB* p)
{     int value;
	ListNodePtr newPtr;
	ListNodePtr prevPtr;
	ListNodePtr currPtr;
      
	newPtr = (ListNodePtr)malloc(sizeof(ListNode));
	newPtr->nextPtr=NULL;
	newPtr->data = p;
      

       /*	strcpy(value, newPtr->data->pcb_priority);
	 */

	 value =  newPtr->data->pcb_priority;
	if(newPtr !=NULL){

		prevPtr = NULL;
		currPtr = sPtr->head;

		if (currPtr == NULL) {
			sPtr->head = newPtr;
			return;
		}
		while(currPtr != NULL && currPtr->data->pcb_priority<value)
		{
			prevPtr = currPtr;
			currPtr = currPtr ->nextPtr;
		}

		if(prevPtr == NULL)
		{
			newPtr -> nextPtr = sPtr->head;
			sPtr->head = newPtr;
		}
		else if( prevPtr != NULL && currPtr != NULL)
		{
			prevPtr -> nextPtr = newPtr;
			newPtr -> nextPtr = currPtr;
		}
		else if(prevPtr != NULL && currPtr == NULL)
		{
		   prevPtr->nextPtr = newPtr;
	       sPtr->tail = newPtr;
		}
	}
	else
	{
		printf("Not inserted. No memory avaliable.\n");
	}
}

void insertfifo(ListPtr sPtr, PCB* p){
      int value;
	ListNodePtr newPtr;
	//ListNodePtr head = NULL;
	//ListNodePtr tail = NULL;
	ListNodePtr prevPtr;
	ListNodePtr currPtr;

      newPtr = (ListNodePtr)malloc(sizeof(ListNode));
	newPtr->nextPtr=NULL;
	newPtr->data = p;

       /*	strcpy(value, newPtr->data->pcb_priority);
	 */

	 value =newPtr->data->pcb_priority;
	if(newPtr != NULL){
		prevPtr=NULL;
		currPtr=sPtr->tail;

		if(isEmpty(sPtr)){
            printf("empty\n");
			sPtr->head=newPtr;
			sPtr->tail=newPtr;
		}
		else{
			prevPtr=currPtr;
			//currPtr=currPtr->nextPtr;
			prevPtr->nextPtr=newPtr;
			sPtr->tail=newPtr;
		}
	}
	else{
		printf("%s is not inserted. No memory available.\n", value);
	}
}

PCB* find(ListPtr sPtr, char *value)
{
	ListNodePtr prevPtr=NULL;
	ListNodePtr currPtr;
      PCB* p = NULL;
     
      currPtr = sPtr->head; 
     while(currPtr != NULL && strcmp(currPtr ->data->pcb_name, value) !=0)
		{
			prevPtr = currPtr;
			currPtr = currPtr ->nextPtr;
		}
     if(currPtr==NULL)
      return NULL;
     else 
    {
     p= (PCB*)currPtr->data;
     return p;
    }
}


PCB* findall (char *value)
{
PCB *p=NULL; 
if ((p=find(ReadyQueue, value))!=NULL)
{ 
return p; 
}

// find it in other queue, set priority only
else if ((p=find(BlockQueue, value))!=NULL)
    return p; 
 else if ((p=find(SusReadyQueue, value))!=NULL)
    return p; 
   else if ((p=find(SusBlockQueue, value))!=NULL)
   return p;  
 else 
   {
   printf( "Can't find this PCB\n");
   return NULL;  
  }

}



PCB* move(ListPtr sPtr, char *value)
{
	ListNodePtr tempPtr=NULL;
	ListNodePtr prevPtr=NULL;
	ListNodePtr currPtr;
      PCB* p = NULL;

	if(strcmp(value, sPtr -> head ->data->pcb_name) == 0)
	{

		tempPtr =sPtr->head;
		sPtr->head=sPtr->head->nextPtr;
            p= (PCB*)tempPtr->data;
		return p;


	}
	else
	{
		prevPtr = sPtr->head;
		currPtr = prevPtr->nextPtr;

		while(currPtr != NULL && strcmp(currPtr ->data->pcb_name, value) !=0)
		{
			prevPtr = currPtr;
			currPtr = currPtr ->nextPtr;
		}

		if(currPtr != NULL)
		{
			tempPtr = currPtr;
			prevPtr -> nextPtr = currPtr -> nextPtr;
                  sPtr->tail=prevPtr; 
			p= (PCB*)tempPtr->data;
		      return p;


		}
	}
	return NULL;

}


int dele(ListPtr sPtr, char *value)
{
	ListNodePtr tempPtr=NULL;
	ListNodePtr prevPtr=NULL;
	ListNodePtr currPtr;

	if(strcmp(value, sPtr -> head ->data->pcb_name) == 0)
	{

		tempPtr =sPtr->head;
		sPtr->head = sPtr->head->nextPtr;
		free(tempPtr->data);
		free(tempPtr);
		return 1;
	}
	else
	{
		prevPtr = sPtr->head;
		currPtr = prevPtr->nextPtr;

		while(currPtr != NULL && strcmp(currPtr->data->pcb_name, value) !=0)
		{
			prevPtr = currPtr;
			currPtr = currPtr ->nextPtr;
		}

		if(currPtr != NULL)
		{
			tempPtr = currPtr;
			prevPtr -> nextPtr = currPtr -> nextPtr;
                  sPtr->tail=prevPtr; 
			free(tempPtr->data);
			free(tempPtr);
			return 1;
		}
	}
	return 0;
}

int isEmpty(ListPtr sPtr)
{
	return (sPtr->head == NULL);
}


void block (char *value ){
//move from ready to blocked
// find and return the PCB in ready queue
PCB* p; 
p = move(ReadyQueue,value);
if (p!=NULL)
{ 
strcpy(p->pcb_state,"blocked");
insertfifo(BlockQueue,p); 
}
else
printf ("block error,Can't find it in the ready queue.\n");

}

void unblock(char *value){
//move from blocked  to ready or suspended-blocked to suspended-ready 
PCB* p;
if ((p=move(BlockQueue,value))!=NULL)
  {
     strcpy(p->pcb_state,"ready");
     insert(ReadyQueue,p);
  }
else if ((p=move(SusBlockQueue,value))!=NULL)
  {
    strcpy(p->pcb_state,"suspended-ready");
    insertfifo(SusReadyQueue,p); 
  }
 else
  {
 printf ("unblock Error,can't find it int the blocked queue\n"); 
  }

}

void suspend(char *value){
//move from blocked  to suspended-blocked or ready to suspended-ready 
PCB* p;
if ((p=move(BlockQueue,value))!=NULL)
  {
      strcpy(p->pcb_state,"suspended-blocked");
      insertfifo(SusBlockQueue,p);
  }
else if ((p=move(ReadyQueue,value))!=NULL)
  {
    strcpy(p->pcb_state,"suspended-ready");
    insertfifo(SusReadyQueue,p); 
  }
 else
  {
 printf ("suspend Error,can't find the in in the active queue.\n"); 
  }

}

void resume(char *value){
//move from  suspended-blocked to blocked or suspended-ready to ready
PCB* p;
if ((p=move(SusBlockQueue,value))!=NULL)
  {
     strcpy(p->pcb_state,"blocked");
     insertfifo(BlockQueue,p);
  }
else if ((p=move(SusReadyQueue,value))!=NULL)
  {
  strcpy(p->pcb_state,"ready");
  insert(ReadyQueue,p); 
  }
 else
  {
 printf ("resume Error,can't find the in in the active queue.\n"); 
  }

}



void print_pcb(PCB* p ){
printf("%s\t",p->pcb_name);              
printf("%d\t", p->pcb_class);              
printf("%s\t", p->pcb_state);             
printf("%d\n", p->pcb_priority);
} 


void printList(char *queueName)
{
	ListNodePtr next;
	ListPtr currPtr;

	if(strcmp(queueName, "ready") ==0){ currPtr = ReadyQueue;

	if(currPtr->head == NULL)
	{

		printf("List is empty.\n");
	}
	else
	{
		next = currPtr->head;
		while(next != NULL)
		{
			print_pcb(next->data);
			next = next->nextPtr;
		}
		printf("\n");
	}
	}

	else if(strcmp(queueName, "blocked") ==0){ currPtr = BlockQueue;

	if(currPtr->head == NULL)
	{
		printf("List is empty.\n");
	}
	else
	{
		next = currPtr->head;
		while(next != NULL)
		{
			print_pcb(next->data);

			next = next->nextPtr;
		}
		printf("\n");
	}
	}
	else if(strcmp(queueName, "suspended-ready") ==0){ currPtr = SusReadyQueue;

	if(currPtr->head == NULL)
	{
		printf("List is empty.\n");
	}
	else
	{
		next = currPtr->head;
		while(next != NULL)
		{
			print_pcb(next->data);

			next = next->nextPtr;
		}
		printf("\n");
	}
	}
	else if(strcmp(queueName, "suspended-blocked") ==0){ currPtr = SusBlockQueue;

	if(currPtr->head == NULL)
	{
		printf("List is empty.\n");
	}
	else
	{
		next = currPtr->head;
		while(next != NULL)
		{
			print_pcb(next->data);

			next = next->nextPtr;
		}
		printf("\n");
	}
	}
	else{
		printf("Invalid Process State!\n");
		return;
	}
}


void setpriority(char *value, char *priority){
// find this PCB in the ReadyQueue first,set priority and reinsert
PCB* p=NULL; 
if (find(ReadyQueue, value)!=NULL)
{ 
p = move(ReadyQueue,value);
p->pcb_priority = atoi(priority);
insert(ReadyQueue,p);
}

// find it in other queue, set priority only
else 
{
 if ((p=find(BlockQueue, value))!=NULL)
     p->pcb_priority = atoi(priority);
 else if ((p=find(BlockQueue, value))!=NULL)
       p->pcb_priority = atoi(priority);
   else if ((p=find(BlockQueue, value))!=NULL)
     p->pcb_priority = atoi(priority);
 else 
   printf( "Can't find this PCB\n");
}

}
